package org.foj.view {
import flash.events.EventDispatcher;
import flash.events.MouseEvent;

import mx.collections.ArrayCollection;

import mx.events.ItemClickEvent;

import mx.events.ListEvent;

import org.flexunit.async.Async;
import org.foj.dto.Issue;
import org.foj.event.EventDispatcherFactory;
import org.foj.event.UIEvent;
import org.hamcrest.assertThat;
import org.hamcrest.object.equalTo;

public class MasterViewTest {

  private var target:MasterView;
  private var eventDispatcher:EventDispatcher;

  [Before(async,ui)]
  public function setUp():void {
    target = new MasterView();
    target.initialize();
    eventDispatcher = EventDispatcherFactory.getEventDispatcher();
  }

  [Test]
  public function issuesPropertySetsDataProviderOnDataGrid():void {
    var dataProvider:ArrayCollection = new ArrayCollection();
    var issue1:Issue = new Issue();
    issue1.id = 1;
    dataProvider.addItem(issue1);

    var issue2:Issue = new Issue();
    issue2.id = 5;
    dataProvider.addItem(issue2);

    target.issues = dataProvider;

    assertThat(target.masterViewDataGrid.dataProvider, equalTo(dataProvider));
  }

  [Test]
  public function resetIssuesGridRemovesAllDataFromDataGrid():void {
    var dataProvider:ArrayCollection = new ArrayCollection();
    var issue1:Issue = new Issue();
    issue1.id = 1;
    dataProvider.addItem(issue1);

    var issue2:Issue = new Issue();
    issue2.id = 5;
    dataProvider.addItem(issue2);

    target.issues = dataProvider;
    target.masterViewDataGrid.selectedIndex = 2;

    target.resetIssuesGrid();

    assertThat(target.masterViewDataGrid.selectedIndex, equalTo(-1));
  }

  [Test(async)]
  public function refreshButtonClickTriggersUIEvent():void {
    Async.proceedOnEvent(this, eventDispatcher, UIEvent.REFRESH_ISSUES_BUTTON_CLICKED, 5000);

    target.refreshIssuesButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
  }

  [Test(async)]
  public function testClickRemoveIssueButton():void {
    Async.proceedOnEvent(this, eventDispatcher, UIEvent.REMOVE_ISSUE_BUTTON_CLICKED, 5000);
    var dataProvider:ArrayCollection = new ArrayCollection();
    var issue1:Issue = new Issue();
    issue1.id = 1;
    dataProvider.addItem(issue1);

    var issue2:Issue = new Issue();
    issue2.id = 5;
    dataProvider.addItem(issue2);

    target.issues = dataProvider;
    target.masterViewDataGrid.selectedIndex = 2;

    target.removeIssueButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
  }

  [Test(async)]
  public function selectingDataGridItemTriggersUIEvent():void {
    Async.proceedOnEvent(this,
        eventDispatcher,
        UIEvent.SELECTED_ISSUE_CHANGED, 5000);

    var dataProvider:ArrayCollection = new ArrayCollection();
    var issue1:Issue = new Issue();
    issue1.id = 1;
    dataProvider.addItem(issue1);

    var issue2:Issue = new Issue();
    issue2.id = 5;
    dataProvider.addItem(issue2);

    target.issues = dataProvider;

    target.masterViewDataGrid.dispatchEvent(
        new ListEvent(ListEvent.ITEM_CLICK, false, false, 1, 2));
  }
}
}